package com.sinosoft.perfect.common.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.sinosoft.perfect.system.license.bean.LicenseContainerBean;

public class LoginValidateFilter implements Filter {
	@Override
	public void destroy() {
		// noting
	}
	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain filterChain) throws IOException, ServletException {
		if(validateURI(servletRequest, servletResponse)) filterChain.doFilter(servletRequest, servletResponse);
	}
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		//filterConfig.getServletContext();
		// noting
	}
	
	
	private LicenseContainerBean licenseContainerBean;
	
	public LicenseContainerBean getLicenseContainerBean() {
		return licenseContainerBean;
	}

	public void setLicenseContainerBean(LicenseContainerBean licenseContainerBean) {
		this.licenseContainerBean = licenseContainerBean;
	}
	
	private boolean validateURI(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException{
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		boolean result = false;
		if(validateLicense(request)) 
			result = true;
		else 
			request.getRequestDispatcher("/page/system/license.jsp").forward(servletRequest, servletResponse);
		if(result == true){
			if(!validateLoginSession(request)) {
				request.getRequestDispatcher("/page/system/login.jsp").forward(servletRequest, servletResponse);
				result = false;
			}
		}
		return result;
	}
	
	private boolean validateLicense(HttpServletRequest request){
		String uri = request.getRequestURI();
		boolean result = true;
		for(String necessaryURI : getNecessaryValidationLicenseURI()){
			if(uri.endsWith(necessaryURI)){
				if(!isRightLicense()) {
					result = false;
					for(String unnecessaryURI : getUnnecessaryValidationLicenseURI()){
						if(uri.endsWith(unnecessaryURI)){
							result = true;
							break;
						}
					}
				}
				break;
			}
		}
		return result;
	}
	
	
	private boolean validateLoginSession(HttpServletRequest request){
		String uri = request.getRequestURI();
		boolean result = false;
		for(String unnecessaryURI : getUnnecessaryValidationLoginSessionURI()){
			if(uri.endsWith(unnecessaryURI)){
				result = true;
				break;
			}
		}
		if(!result){
			result = isRightLoginSession(request);
		}
		return result;
	}
	
	private List<String> getUnnecessaryValidationLoginSessionURI(){
		List<String> uriList = new ArrayList<String>();
		uriList.add(".css");
		uriList.add(".js");
		uriList.add(".gif");
		uriList.add(".png");
		uriList.add(".jpg");
		uriList.add("/");
		uriList.add("login.jsp");
		uriList.add("LoginAction!login.action");
		uriList.add("validateLogin.validate.dwr");
		uriList.add("licenseDataService.initLicenseForShow.dwr");
		uriList.add("licenseDataService.registerLicense.dwr");
		uriList.add("licenseDataService.deleteLicense.dwr");		
		return uriList;
	}
	
	private List<String> getNecessaryValidationLicenseURI(){
		List<String> uriList = new ArrayList<String>();
		uriList.add(".jsp");
		uriList.add(".action");
		return uriList;
	}
	
	private List<String> getUnnecessaryValidationLicenseURI(){
		List<String> uriList = new ArrayList<String>();
		uriList.add("license.jsp");
		return uriList;
	}
	
	private boolean isRightLoginSession(HttpServletRequest request){
		return request.getSession().getAttribute("user")==null?false:true;
	}
	
	private boolean isRightLicense(){
		return licenseContainerBean.getLicenseStatus().equals(LicenseContainerBean.RIGHT_LICENSE);
	}
	
}
